package cn.e4j.modular.api.controller;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.AzyyService;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuan_rz on 2018/04/10.
 */
@RestController
@RequestMapping("api/wx/azyy")
public class AzyyController extends ControllerSupport {

    @Autowired
    private AzyyService azyyService;

    @Autowired
    SystemServiceImpl systemService;


    /**
     * 获取已购设备列表
     *
     * @return
     */
    @RequestMapping(value = "getListData")
    public ResponseData getDeviceListData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        List<Dto> list = new ArrayList<Dto>();
        try {
            Dto sys003 = systemService.getSys003(currentUser);
            inDto.put("orgIds",(List<Dto>)sys003.get("ORGS"));
            list = azyyService.getListData(wapper.getPage(),inDto);
            list.forEach(dto->{
                dto.put("GWAM007",azyyService.getGwam007(dto));
                Dto gwalDto = azyyService.getGwal(dto);
                if (ToolUtil.isNotEmpty(gwalDto))
                dto.putAll(gwalDto);
            });
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }
}
